#!/bin/sh
#智慧庭审-基础环境配置
####################################################安装变量####################################################
source_dir=/root/install_base/				                    #所有压缩包所在目录(绝对路径)
source_data=/root/data/
target_dir=/opt/				                            #安装目录(绝对路径)

host_ip=192.168.157.200
zookeeper_ip=192.168.157.200:2181			              #zookeeper IP地址:端口
zookeeper_net=192.168.157			                      #zookeeper 网段
group=asr				                                    #服务统一组名
####################################################安装变量####################################################
function systemSet() {
    #安全
    echo -ne "#### 系统配置......"
    systemctl stop firewalld
    systemctl disable firewalld >> /install.log 2>&1
    sed -i "s/SELINUXTYPE=enforcing/SELINUXTYPE=disabled/g" /etc/selinux/config
    setenforce 0 >> /install.log 2>&1

    #工具包安装
    yum -y install ntpdate autoconf dos2unix unzip >> /install.log 2>&1

    #设置时区
    timedatectl set-timezone Asia/Shanghai
    ntpdate time.windows.com >> /install.log 2>&1
    hwclock --systohc                                   #系统时间同步到硬件时间
    echo " OK"
}

function upPack() {
    #软件包解压
    echo -ne "#### 安装包解压...."
    for i in `ls ${source_dir}*.gz`
    do
    	tar xf ${i} -C ${target_dir}
    done
    for i in `ls ${source_dir}*.tgz`
    do
    	tar xf ${i} -C ${target_dir}
    done
    for i in `ls ${source_dir}*.zip`
    do
    	unzip  ${i} -d ${target_dir} >> /install.log 2>&1
    done
    #for i in `ls ${source_dir}*.tar`
    #do
    #	tar xf ${i} -C ${target_dir}
    #done

    #验证 减去一个包
    allPackage=$(( `ls ${source_dir} | wc -l`-1))
    openPackage=$(ls ${target_dir} | wc -l )
    if [ $allPackage -eq $openPackage ];then
       echo " OK"
    else if [ $allPackage -le $openPackage ]; then
            echo "... OK 解压完成,${target_dir} 目录下有其它文件!"
         else
            echo "... ERROR 部分软件包未解压,请手动完成！"
         fi
    fi
}

function jdk() {
    #JDK
    echo -ne "#### 配置JDK......"
    echo "#基础环境脚本配置" >> /etc/profile
    echo "export JAVA_HOME=${target_dir}jdk1.8.0_101" >> /etc/profile
    echo "export PATH=\${JAVA_HOME}/bin:\$PATH" >> /etc/profile
    source /etc/profile
    #验证
    java -version >> /install.log 2>&1 && echo "... OK" || echo "... ERROR"
}

function docker() {
    #docker
    echo -ne "#### 安装docker..."
    cd ${target_dir}docker/ && yum -y install * >> /install.log 2>&1
    systemctl start docker && systemctl enable docker >> /install.log 2>&1
    #验证
    docker ps >> /install.log 2>&1 && echo "... OK" || echo "... ERROR"

}

function zookeeper() {
    #zookeeper
    echo -ne "#### 安装zookeeper"
    docker load < ${target_dir}zookeeper/zookeeper-3.4.8.image >> /install.log 2>&1
    docker run -d --net=host --restart=always --privileged=true --name yj-zookeeper-2181 -v ${target_dir}zookeeper/conf:${target_dir}zookeeper/conf -v ${target_dir}zookeeper/data:${target_dir}zookeeper/data -v ${target_dir}zookeeper/log:${target_dir}zookeeper/log zookeeper >> /install.log 2>&1
    mv ${target_dir}zookeeper-3.4.8/ /usr/local/ >> /install.log 2>&1
    #验证
    sleep 30
    /usr/local/zookeeper-3.4.8/bin/zkCli.sh ls / >> /install.log 2>&1 && echo "... OK" || echo "... ERROR"

    #zk导入数据
    cat ${source_data}/zk-asr4.7.0_icp2.3.0.txt | /usr/local/zookeeper-3.4.8/bin/zkCli.sh -server ${zookeeper_ip} >> /install.log 2>&1

}

function tomcat() {
    #tomcat
    echo -ne "#### 安装Tomcat..."
    tomcatExec=${target_dir}$(ls ${target_dir} | grep apache)/               #tomcat家目录
    cp ${source_dir}asr-web.war ${tomcatExec}webapps/
    chmod +x ${tomcatExec}bin/startup.sh
    dos2unix ${tomcatExec}bin/startup.sh >> /install.log 2>&1
    sh ${tomcatExec}bin/startup.sh >> /install.log 2>&1
    sleep 10
    sed -i "s/^configAddr=*.*.*.*:*/configAddr=${zookeeper_ip}/g" ${tomcatExec}webapps/asr-web/WEB-INF/classes/asr.properties
    sed -i "s/^registry.address=*.*.*.*:*/registry.address=${zookeeper_ip}/g" ${tomcatExec}webapps/asr-web/WEB-INF/classes/asr.properties
    sed -i "s/asr.svr.group=[a-z]*/asr.svr.group=${group}/g" ${tomcatExec}webapps/asr-web/WEB-INF/classes/asr.properties
    sh ${tomcatExec}bin/startup.sh>> /install.log 2>&1
    #验证
    curl http://localhost:8080/ >> /install.log 2>&1 && echo "... OK" || echo "... ERROR"

}

function mysql() {
    #mysql  root密码123456
    echo -ne "#### 安装MySQL...."
    mysqlExec=${target_dir}$(ls ${target_dir} | grep mysql)/               #mysql家目录
    useradd -s /sbin/nologin mysql >> /install.log 2>&1
    ln -s ${mysqlExec} ${target_dir}mysql
    cd ${target_dir}mysql && chown -R root:mysql .
    echo "[mysqld]
    basedir=${target_dir}mysql
    datadir=${target_dir}mysql/data
    port=3306
    sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
    default-storage-engine=INNODB
    lower_case_table_name=1
    collation-server=utf8_general_ci
    character-set-server=utf8
    wait_timeout=1200
    interactive_timeout=1200

    [mysql]
    default_character_set=utf8
    " > /etc/my.cnf
    cd ${target_dir}mysql/ && ./scripts/mysql_install_db --user=mysql --datadir=${target_dir}mysql/data >> /install.log 2>&1
    cp -a ${target_dir}mysql/support-files/mysql.server /etc/rc.d/init.d/mysqld >> /install.log 2>&1
    service mysqld start >> /install.log 2>&1 && chkconfig mysqld on >> /install.log 2>&1
    #chkconfig --list | grep mysqld
    ln -s ${target_dir}mysql/bin/* /usr/bin/ >> /install.log 2>&1
    mysqladmin -uroot password 123456 >> /install.log 2>&1
    #验证
    mysql -uroot -p123456 -e "show databases;" >> /install.log 2>&1 && echo "... OK" || echo "... ERROR"

}
function mongo() {
    #mongoDB (port)
    echo -ne "#### 安装mongoDB.."
    docker load < ${target_dir}mongodb/mongo-3.2.3.image >> /install.log 2>&1
    sed -i "s/bind_ip=*.*.*.*/bind_ip=${host_ip}/g" ${target_dir}mongodb/conf/mongo.conf
    sed -i "s/port=[0-9]*/port=37017/g" ${target_dir}mongodb/conf/mongo.conf
    docker run -d --net=host --restart=always --privileged=true --name yj-mongo-37017 -v ${target_dir}mongodb/conf:${target_dir}mongodb/conf -v ${target_dir}mongodb/data:${target_dir}mongodb/data -v ${target_dir}mongodb/log:${target_dir}mongodb/log mongo >> /install.log 2>&1
    #验证
    tar xvf ${source_data}mongodb-linux-x86_64.tar.gz -C /usr/local/ >> /install.log 2>&1
    echo "show dbs;" | /usr/local/mongodb/bin/mongo ${host_ip}:37017 >> /install.log 2>&1 && echo "... OK" || echo "... ERROR"
}

function redis() {
    #redis
    echo -ne "#### 安装redis...."
    docker load < ${target_dir}redis/redis-3.0.5.image >> /install.log 2>&1
    docker run -d --net=host --restart=always --privileged=true --name yj-redis-16379 -v ${target_dir}redis/conf:${target_dir}redis/conf -v ${target_dir}redis/data:${target_dir}redis/data -v ${target_dir}redis/log:${target_dir}redis/log redis >> /install.log 2>&1
    #验证
    docker ps  | grep yj-redis-16379 >> /install.log 2>&1 && echo "... OK" || echo "... ERROR"
}

function insertData() {
    #date-以上代码没问题
    	#mysql
    mysql -uroot -p123456 -e "CREATE DATABASE asr_db /*!40100 DEFAULT CHARACTER SET utf8 COLLATE utf8_bin */;" >> /install.log 2>&1
    mysql -uroot -p123456 -e "GRANT ALL PRIVILEGES ON *.* TO 'asr'@'127.0.0.1' IDENTIFIED BY 'asr_2w4r123' WITH GRANT OPTION;" >> /install.log 2>&1
    mysql -uroot -p123456 -e "GRANT ALL PRIVILEGES ON *.* TO 'asr'@'${host_ip}' IDENTIFIED BY 'asr_2w4r123' WITH GRANT OPTION;" >> /install.log 2>&1
    mysql -uroot -p123456 -e "GRANT ALL PRIVILEGES ON *.* TO 'asr'@'localhost' IDENTIFIED BY 'asr_2w4r123' WITH GRANT OPTION;" >> /install.log 2>&1
    mysql -uroot -p123456 -e "GRANT ALL PRIVILEGES ON yunjia.* to 'yunjia'@'%' IDENTIFIED BY 'asr_2w4r123';" >> /install.log 2>&1
    mysql -uroot -p123456 -e "GRANT ALL PRIVILEGES ON *.* TO 'asr'@'%' IDENTIFIED BY 'asr_2w4r123' WITH GRANT OPTION;" >> /install.log 2>&1
    mysql -uroot -p123456 -e "flush privileges;" >> /install.log 2>&1
    mysql -uroot -p123456 asr_db < ${source_data}asr_db-2022-04-08.sql >> /install.log 2>&1
      #mongoDB
    /usr/local/mongodb/bin/mongoimport -h ${host_ip}:37017 -u fileDB -p file@123456 -d fileDB -c asr_evidence_law_item --file ${source_data}asr_evidence_law_item.json --type json
    /usr/local/mongodb/bin/mongoimport -h ${host_ip}:37017 -u fileDB -p file@123456 -d fileDB -c asr_evidence_law_item_desc --file ${source_data}asr_evidence_law_item_desc.json --type json

    cat ${source_data}/mongo_data.txt | /usr/local/mongodb/bin/mongo ${host_ip}:37017
}

systemSet
upPack
jdk
docker
zookeeper
tomcat
mysql
mongo
redis
insertData